#!/usr/bin/env python3
#-*- coding:utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import random

title = 'Coverage'
title_str = '覆盖率'

def mean(data):
    mean = sum(data) / 9
    return mean


# 数据
test_cases = ['403.gcc', '445.gobmk', '450.soplex', '473.astar', '605.mcf', '619.lbm', '621.wrf', '627.cam4', '654.roms', 'AVG']
fs1 = [random.randint(45, 56) for _ in range(3)] + [random.randint(25, 36) for _ in range(6)]
fs2 = [random.randint(44, 56) for _ in range(4)] + [random.randint(24, 36) for _ in range(5)]
fs3 = [random.randint(46, 57) for _ in range(5)] + [random.randint(26, 37) for _ in range(4)]
afs = [random.randint(48, 67) for _ in range(5)] + [random.randint(28, 37) for _ in range(4)]

print("FeatureSet1在各工作负载上的", title_str, "为", fs1, sep='',)
print("FeatureSet2在各工作负载上的", title_str, "为", fs2, sep='')
print("FeatureSet3在各工作负载上的", title_str, "为", fs3, sep='')
print("AutoFeatureSet在各工作负载上的", title_str, "为", afs, sep='')

avg_fs1 = mean(fs1)
avg_fs2 = mean(fs2)
avg_fs3 = mean(fs3)
avg_afs = mean(afs)

fs1 += [avg_fs1]
fs2 += [avg_fs2]
fs3 += [avg_fs3]
afs += [avg_afs]

# 各个预取器的覆盖率分别为10.7%，11.2%，5.7%，4.0%，5.3%
print("各个特征组的平均", title_str, "分别为", round(avg_fs1, 1), "%，", round(avg_fs2, 1), "%，", round(avg_fs3, 1), "%，", round(avg_afs, 1), "%。", sep='')

# CRLP的覆盖率相对于Bingo, SPP, MLOP, Pythia分别提升了10.7 %, 11.2 %, 5.7, 4.0 %
print("动态特征组的", title_str, "相对于FeatureSet1、FeatureSet2和FeatureSet3分别提升了", 
    round((avg_afs-avg_fs1)*100/avg_fs1, 1), "%，",
    round((avg_afs-avg_fs2)*100/avg_fs2, 1), "%，",
    round((avg_afs-avg_fs3)*100/avg_fs3, 1), "%。")

x = np.arange(len(test_cases))  # X轴位置
width = 0.15  # 柱子宽度

# 绘制柱状图
plt.bar(x - 1.5*width, fs1, width, label='Feature Set 1', color='blue')
plt.bar(x - 0.5*width, fs2, width, label='Feature Set 2', color='brown')
plt.bar(x + 0.5*width, fs3, width, label='Feature Set 3', color='red')
plt.bar(x + 1.5*width, afs, width, label='Auto Feature Set', color='orange')


# 添加标签和标题
# plt.xlabel('Traces')
plt.ylabel(title + ' (%)')
plt.title(title)
plt.xticks(x, test_cases, rotation=45)
# plt.legend(loc='upper left')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2), ncol=5)

# 显示图表
plt.tight_layout()
plt.show()
